package leetcode

/*
 * @lc app=leetcode.cn id=706 lang=golang
 *
 * [706] 设计哈希映射
 */

// @lc code=start
const base = 769

// MyHashMap ...
type MyHashMap struct {
	data [][][2]int
}

// ConstructorMyHashMap ...
/** Initialize your data structure here. */
func ConstructorMyHashMap() MyHashMap {
	return MyHashMap{make([][][2]int, base)}
}

// Put ...
/** value will always be non-negative. */
func (hm *MyHashMap) Put(key int, value int) {
	h := key % base
	if lst := hm.data[h]; lst == nil {
		hm.data[h] = make([][2]int, 0)
	}
	for i, v := range hm.data[h] {
		if v[0] == key {
			hm.data[h][i][1] = value
			return
		}
	}
	hm.data[h] = append(hm.data[h], [2]int{key, value})
}

// Get ...
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
func (hm *MyHashMap) Get(key int) int {
	h := key % base
	for _, v := range hm.data[h] {
		if v[0] == key {
			return v[1]
		}
	}
	return -1
}

// Remove ...
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
func (hm *MyHashMap) Remove(key int) {
	h := key % base
	for i, v := range hm.data[h] {
		if v[0] == key {
			hm.data[h] = append(hm.data[h][:i], hm.data[h][i+1:]...)
		}
	}
}

/**
 * Your MyHashMap object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Put(key,value);
 * param_2 := obj.Get(key);
 * obj.Remove(key);
 */
// @lc code=end
